package com.song.onetomany.service.impl;

import com.github.pagehelper.PageInfo;
import com.song.onetomany.mapper.UserMapper;
import com.song.onetomany.pojo.Role;
import com.song.onetomany.pojo.User;
import com.song.onetomany.service.UserService;
import com.song.onetomany.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2023/9/11
 * @Version 1.0
 **/
public class UserServiceImpl  implements UserService {
    private SqlSession session = MyBatisUtil.getInstance();
    private UserMapper userMapper = session.getMapper(UserMapper.class);

    @Override
    public PageInfo<User> page_user(String offset, String pagesize) {
        //解决null的问题
        offset = offset == null ? "1": offset;
        pagesize = pagesize == null ? "3": pagesize;
        int start= Integer.parseInt(offset);
        int size= Integer.parseInt(pagesize);
        //以下代码的作用为limit注入参数：limit 6,5
        com.github.pagehelper.PageHelper.startPage(start,size);
        PageInfo<User> pager=new PageInfo<>(this.userMapper.findUsers());
        pager.getTotal();//总记录数：.total
        pager.getList();//页面数据集合 list
        pager.isHasNextPage();//存在下页
        pager.isHasPreviousPage();//上一页
        pager.getPages();//总页数: pages
        return pager;
    }

    @Override
    public List<User> users() {
        //查询不需要事务
        return this.userMapper.findUsers();
    }

    @Override
    public List<Role> roles() {
        return this.userMapper.findRoles();
    }

    @Override
    public boolean save(User user, String[] ids) {
        System.out.println("service.............................>"+user);
        int m=0;//接收受影响的行数
        if(user.getId()==null || user.getId()==0) {
            //插入数据到用户表
            m = this.userMapper.addUser(user);
            System.out.println("新增用户后：" + user);
            // 插入数据到用户角色表（中间表）
            m += this.userMapper.addUserRole(user.getId(), ids);
            if (m >= 2) {
                this.session.commit();
                return true;
            } else {
                this.session.rollback();
            }
        }else{
            //修改
            //获取用户的id
            int id = user.getId();
            //删除原中间表的记录
            m= this.userMapper.deleteUserRloe(id);
            //修改用户信息
            m+= this.userMapper.updateUser(user);
            //插入新的角色到中间表
            m += this.userMapper.addUserRole(id, ids);
            if(m>=3){
                this.session.commit();
                return true;
            }else{
                this.session.rollback();
            }
        }
        return false;
    }

    @Override
    public User findUserBy(String id) {
        return this.userMapper.findUserById(id);
    }

    @Override
    public boolean delete(String ids) {
        int m=0;
        //删除中间表(批量删除)
        String[] arr = ids.split(",");
         m=this.userMapper.batchDelUid(arr);
        System.out.println("--------------删除中间表-->"+m);
        //删除用户表
        m+= this.userMapper.batchUser(arr);
        System.out.println("删除用户表："+m);
        if(m>=2){
            this.session.commit();
            return  true;
        }else{
            this.session.rollback();
        }
        return false;
    }
}
